"""
翻页依赖于上一页的参数  如果某个之母的页数不对
删除该字母的所有html网页 重新下载
"""
import os
import sys

from xjlibrary.mrequest.baserequest import BaseRequest
from xjlibrary.mysqlmoudel.simple.mysqlclient import MySqlDbConnect, ExeSqlList, SelctSqlFromDB, ExeSqlToDB
from xjlibrary.tools.BaseFile import BaseFile, SingleWriteFile, SingleAddFile
from xjlibrary.tools.BaseUrl import BaseUrl

baseurl = "http://10.38.48.163/tingbook/"
HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Host": "10.38.48.163",
    "Proxy-Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
ListSql = []
curPath = BaseFile.get_path_absolute(__file__)
TopPath = BaseFile.get_top_path(curPath, -2)
sPath = BaseFile.getNewPath(TopPath, "download", "tianfang", "download", "chapter")
sPath2 = BaseFile.getNewPath(TopPath, "download", "tianfang", "download", "chapter2")
proxyss = {
    "http": "192.168.30.176:8033",
    "https": "192.168.30.176:8033",
}


def requesthome(url):
    BoolResult, errString, r = BaseRequest(url, mark="left_nav", headers=HEADERS, proxies=proxyss, timeout=45)
    if not BoolResult:
        print("请检查失败原因:" + errString)
        sys.exit(-1)
    return r

def requestcount(url):
    BoolResult, errString, r = BaseRequest(url, mark="",endstring="", headers=HEADERS, proxies=proxyss, timeout=45)
    if not BoolResult:
        print("请检查失败原因:" + errString)
        sys.exit(-1)
    return r


# 数据库链接
def MajorDbConnect():
    return MySqlDbConnect(curPath, "db.ini")


def InsertSql():
    global nCount, ListSql
    conn = MajorDbConnect()
    success, failed = ExeSqlList(ListSql, conn, errExit=True)
    ListSql = list()


def SelectFromDB():
    sql = "select `url`,`titlename`,`filename` from `chapter` where `stat`=0"
    conn = MajorDbConnect()
    rows = SelctSqlFromDB(sql, conn)
    return rows


def UpdateSql(sql):
    conn = MajorDbConnect()
    ExeSqlToDB(sql, conn, errExit=True)


def savefile(r, filePath):
    SingleWriteFile(r.text, filePath)

def savefile2(r, filePath):
    SingleWriteFile(r, filePath)

def savefileadd(r, filePath):
    SingleAddFile(r.text, filePath)

def start():
    for row in SelectFromDB():
        url = baseurl + row[0]
        if not os.path.exists(sPath):
            os.makedirs(sPath)
        if not os.path.exists(sPath2):
            os.makedirs(sPath2)
        filename = row[2].split("_")[0]+"_"+row[1]+"_"+BaseUrl.urlQuery2Dict(url)["id"]+".html"
        filePath = os.path.join(sPath2, filename)
        if os.path.exists(filePath):
            print("文件存在" + filePath)
            sql = "update `chapter` set `stat`=1 where `url`='{}'".format(row[0])
            UpdateSql(sql)
            continue
        filePath = os.path.join(sPath, filename)
        if os.path.exists(filePath):
            print("文件存在" + filePath)
            sql = "update `chapter` set `stat`=1 where `url`='{}'".format(row[0])
            UpdateSql(sql)
            continue
        # 这里有两个请求 一个是页面  一个是实际内容 目前只要实际类容 先屏蔽页面请求
        # r = requesthome(url)
        # savefile(r, filePath)
        url = url.replace("play.aspx", "showContent.aspx")
        r = requestcount(url)
        try:
            savefile2(r.content.decode('utf-8').strip(), filePath)
        except:
            savefile2(r.content.decode('GB18030').strip(), filePath)

        sql = "update `chapter` set `stat`=1 where `url`='{}'".format(row[0])
        UpdateSql(sql)


def init():
    sql = "update `chapter` set `stat`=0"
    UpdateSql(sql)

# 废弃
if __name__ == "__main__":
    # 更新时使用
    # init()
    start()
    InsertSql()
